
package vu.cs.heuristieken;

import java.util.List;
import java.util.LinkedList;

public class HouseList<E> extends LinkedList<E> {
    private List<_HouseCount> houseCounts;

    public HouseList() {
        this.houseCounts = new LinkedList<_HouseCount>();
    }

    public boolean add(E x) {
        super.add(x);

        Class<?> houseType      = x.getClass();
        _HouseCount houseCount = this._getHouseCount(houseType);
        if(null == houseCount) {
            this.houseCounts.add(new _HouseCount(houseType, 1));
        }
        else {
            houseCount.count++;
        }
        return true;
    }

    public int getNHousesOfType(Class<?> houseType) {
        _HouseCount houseCount = this._getHouseCount(houseType);
        return (null != houseCount)? houseCount.count : 0;
    }

    public List<Class<?>> getHouseTypes() {
        List<Class<?>> houseTypes = new LinkedList<Class<?>>();

        for(_HouseCount houseCount : this.houseCounts) {
            houseTypes.add(houseCount.houseType);
        }
        return houseTypes;
    }

    public List<House> getUniqueHouses() {
        List<House> result = new LinkedList<House>();
        for(E house : this) {
            boolean found = false;
            for(House unique : result) {
                if(unique.getClass() == house.getClass()) {
                    found = true;
                    break;
                }
            }

            if(!found) {
                result.add((House) house);
            }
        }
        return result;
    }

    private _HouseCount _getHouseCount(Class<?> houseType) {
        for(_HouseCount houseCount : this.houseCounts) {
            if(houseCount.houseType == houseType) {
                return houseCount;
            }
        }
        return null;
    }

    private class _HouseCount {
        public Class<?> houseType;
        public int      count;

        public _HouseCount(Class<?> houseType, int count) {
            this.houseType = houseType;
            this.count     = count;
        }
    }
}